//
// 多线程高性能计数器while计数循环间隔及计数可靠性
// 结论：QueryPerformanceCounter处理速度极高，每个计数周期内平均可执行查询循环4-8次，精度满足要求

#include <stdio.h>
#include <windows.h>
#include <thread>

LARGE_INTEGER nFreq;
LARGE_INTEGER t[2][100000];

void setNetDelay(int index)
{
    printf("setNetDelay %d %d\n", index, std::this_thread::get_id());
    int i = -1;
    do {
        i++;
        QueryPerformanceCounter(&t[index][i]);//处理速度极高，每个计数周期内平均可执行该循环4-8次，如果间隔时间过长可能会导致内存溢出
//        printf("%d", i);
    } while ((t[index][i].QuadPart - t[index][0].QuadPart) < 10000);//为0.1ms
}

int main() {
    QueryPerformanceFrequency(&nFreq);
    printf("%lld\n", nFreq.QuadPart);
    printf("main %d\n", std::this_thread::get_id());

    std::thread thread0(setNetDelay, 0);
    std::thread thread1(setNetDelay, 1);
    thread0.join();
    thread1.join();

//    Sleep(5);

    for(int i = 0; i < 2; i++) {
        int j = 0;
        while (t[i][j].QuadPart > 0) {
//            printf("%lld - %lld = %lld\n", t[i][j].QuadPart, t[i][0].QuadPart, t[i][j].QuadPart - t[i][0].QuadPart);
            j++;
        }
        printf("%lld - %lld = %lld\n", t[i][j-1].QuadPart, t[i][0].QuadPart, t[i][j-1].QuadPart - t[i][0].QuadPart);
        printf("%d\n\n", j);
    }
    return 0;
}
